package io.litchi.common.base;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * description: 通用字段
 *
 * @author helang
 * @since 2025-09-16
 */
@Data
@MappedSuperclass // 关键：表示这是一个映射超类，不会生成表
@EntityListeners(AuditingEntityListener.class) // 启用 JPA 实体监听器
public abstract class BaseEntity implements Serializable {
    @Serial
    private static final long serialVersionUID = -1781814811415L;

    /**
     * 只要加上了@jakarta.persistence.Version, 会自动开启乐观锁控制
     * <p>!不添加乐观锁控制.
     */
    @Column(columnDefinition = "BIGINT DEFAULT 0 COMMENT '版本号'")
    protected Long version;

    @CreatedDate
    @Column(name = "create_date", columnDefinition = "DATETIME COMMENT '创建时间'")
    protected LocalDateTime createDate;

    @LastModifiedDate
    @Column(name = "update_date", columnDefinition = "DATETIME COMMENT '最后更新时间'")
    protected LocalDateTime updateDate;

    @CreatedBy
    @Column(name = "creator", columnDefinition = "BIGINT COMMENT '创建人'")
    private Long creator;

    @LastModifiedBy
    @Column(name = "updater", columnDefinition = "BIGINT COMMENT '更新人'")
    private Long updater;
}